package com.moli.iov.handle.alarm.biz.real;

import com.moli.iov.base.enums.AlarmAuthEnum;
import com.moli.iov.handle.alarm.enums.AlarmCodeEnum;
import com.moli.iov.handle.alarm.enums.AlarmSourceEnum;
import com.moli.iov.handle.alarm.enums.AlarmTypeEnum;
import com.moli.iov.handle.alarm.enums.AntiShakeEnum;
import com.moli.iov.handle.alarm.model.dto.AlarmDto;
import com.moli.iov.handle.alarm.model.dto.AuthDto;
import com.moli.iov.handle.alarm.model.dto.DisarmAlarmDto;
import com.moli.iov.handle.alarm.service.IAlarmInfoRequestService;
import com.moli.iov.protocol.model.realtime.AlarmReq;
import com.moli.iov.protocol.model.realtime.RealTimeInfoReq;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * <p>
 * 实时故障处理
 * </p>
 *
 * @author: zql
 * @Date: 2019/2/15 9:27
 */
@Slf4j
@Component
public class MalfunctionReal {

    @Autowired
    private IAlarmInfoRequestService alarmInfoRequestService;

    public void handle(RealTimeInfoReq realTimeInfoReq, AuthDto authDto){
        try{
            AlarmReq alarmReq = realTimeInfoReq.getAlarmReq();
            if(alarmReq == null){
                log.error("实时故障处理 失败 AlarmReq 为null vin:{} ", realTimeInfoReq.vin);
                return;
            }

            storingDeviceFault(realTimeInfoReq.vin, realTimeInfoReq, authDto);

            driveMotorFault(realTimeInfoReq.vin, realTimeInfoReq, authDto);

        }catch(Exception e){
            log.error("实时故障处理 异常", e);
        }
    }

    /**
     * 可充电储能装置故障报警
     * @param realTimeInfoReq
     */
    public void storingDeviceFault(String vin, RealTimeInfoReq realTimeInfoReq, AuthDto authDto){
        try{
            AlarmReq alarmReq = realTimeInfoReq.getAlarmReq();
            int count = alarmReq.getStoringDeviceFaultSum();
            if(count > 0){

                if(authDto.getAlarmAuths().contains(AlarmAuthEnum.REAL.getFlag())){
                    log.info("实时故障处理 不监控报警 vin:{}", vin);
                    return;
                }

                log.info("可充电储能装置故障报警 入参 vin：{} alarmReq:{} ",
                        vin, alarmReq);
                double longitude = realTimeInfoReq.vehicleGpsReq != null ? realTimeInfoReq.vehicleGpsReq.longitude : 0;
                double latitude = realTimeInfoReq.vehicleGpsReq != null ? realTimeInfoReq.vehicleGpsReq.latitude : 0;

                alarmInfoRequestService.alarm(new AlarmDto(
                        vin,
                        AlarmCodeEnum.STORING_DEVICE,
                        AlarmTypeEnum.FAULT.getFlag(),
                        AlarmSourceEnum.T_BOX.getFlag(),
                        "",
                        AntiShakeEnum.YES.getFlag(),
                        longitude,
                        latitude));

                return;
            }


            alarmInfoRequestService.disarmAlarm(new DisarmAlarmDto(vin,
                    AlarmCodeEnum.STORING_DEVICE.getFlag(),
                    AntiShakeEnum.YES.getFlag()));
        }catch(Exception e){
            log.error("可充电储能装置故障报警异常  vin:{}, alarmReq:{}",
                    vin, realTimeInfoReq);
        }
    }

    /**
     * 驱动电机故障报警
     * @param realTimeInfoReq
     */
    public void driveMotorFault(String vin, RealTimeInfoReq realTimeInfoReq, AuthDto authDto){
        try{
            AlarmReq alarmReq = realTimeInfoReq.getAlarmReq();
            int count = alarmReq.getDriveMotorFaultSum();
            if(count > 0){

                if(authDto.getAlarmAuths().contains(AlarmAuthEnum.REAL.getFlag())){
                    log.info("驱动电机故障报警 不监控报警 vin:{}", vin);
                    return;
                }

                log.info("驱动电机故障报警 vin：{} 数据：{}",
                             vin, alarmReq);
                double longitude = realTimeInfoReq.vehicleGpsReq != null ? realTimeInfoReq.vehicleGpsReq.longitude : 0;
                double latitude = realTimeInfoReq.vehicleGpsReq != null ? realTimeInfoReq.vehicleGpsReq.latitude : 0;

                alarmInfoRequestService.alarm(new AlarmDto(
                        vin,
                        AlarmCodeEnum.DRIVE_MOTOR,
                        AlarmTypeEnum.FAULT.getFlag(),
                        AlarmSourceEnum.T_BOX.getFlag(),
                        "",
                        AntiShakeEnum.YES.getFlag(),
                        longitude,
                        latitude));

                return;
            }

            alarmInfoRequestService.disarmAlarm(new DisarmAlarmDto(vin,
                    AlarmCodeEnum.DRIVE_MOTOR.getFlag(),
                    AntiShakeEnum.YES.getFlag()));
        }catch(Exception e){
            log.error("驱动电机故障报警 异常 vin:{}, alarmReq:{}",
                        vin, realTimeInfoReq, e);
        }
    }
}
